package com.wind.sqlite.jobRunner;


import com.google.gson.Gson;
import com.wind.httpProxyServer.bean.ProcessConfigBean;
import com.wind.httpProxyServer.manager.HttpProxyServerManager;
import com.wind.sqlite.entity.ProcessConfigTEntity;
import com.wind.sqlite.entity.SysUserEntity;
import com.wind.sqlite.service.ProcessConfigTService;
import com.wind.sqlite.service.SysUserService;
import com.wind.sqlite.tools.WhilteTools;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class JobRunner implements CommandLineRunner {

    private static final Logger logger = LoggerFactory.getLogger(JobRunner.class);

    @Autowired
    private ProcessConfigTService processConfigTService;

    @Autowired
    private SysUserService sysUserService;

    @Override
    public void run(String... args) throws Exception {

        SysUserEntity sysUserEntityQuery =new SysUserEntity();
        sysUserEntityQuery.setUsername("admin");
        SysUserEntity sysUserEntity = sysUserService.queryOne(sysUserEntityQuery);
        //增加admin账号
        if(sysUserEntity==null){
            SysUserEntity newAdminUser =new SysUserEntity();
            newAdminUser.setUsername("admin");
            newAdminUser.setPassword("admin");
            sysUserService.saveUser(newAdminUser);
        }
        //将Whilte设置内存中
        WhilteTools.setWhilteToCache();
        ProcessConfigTEntity processConfigTQuery =new ProcessConfigTEntity();
        processConfigTQuery.setStatus(true);
        List<ProcessConfigTEntity> processConfigTEntitys = processConfigTService.query(processConfigTQuery);

        for(ProcessConfigTEntity processConfigTEntity:processConfigTEntitys){
            Gson gson = new Gson();
            ProcessConfigBean processConfigBean = gson.fromJson(gson.toJson(processConfigTEntity), ProcessConfigBean.class);
            HttpProxyServerManager.start(processConfigBean);
        }



    }
}
